package com.aubrey.threadDemo.create;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPool {
	public static void main(String[] args) {
		Thread t1 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					System.out.println(Thread.currentThread().getName() + ":" + i);
				}
				
			}
		});
//		sigle(t1);
//		fix(t1);
//		cache(t1);
		sche(t1);
	}
	
	public static void sigle(Runnable t1) {
		ExecutorService executor = Executors.newSingleThreadExecutor();
		for (int i = 0; i < 5; i++) {
			executor.execute(t1);
		}
	}
	
	public static void fix(Runnable t1) {
		ExecutorService executor = Executors.newFixedThreadPool(3);
		for (int i = 0; i < 5; i++) {
			executor.execute(t1);
		}
	}
	
	public static void cache(Runnable t1) {
		ExecutorService executor = Executors.newCachedThreadPool();
		for (int i = 0; i < 10; i++) {
			executor.execute(t1);
		}
	}
	
	
	public static void sche(Runnable t1) {
		ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
		long time = 5000;
		
		for (int i = 0; i < 1; i++) {
//			executor.schedule(t1, 1000, TimeUnit.MILLISECONDS);
			executor.scheduleAtFixedRate(t1, 1000, time, TimeUnit.MILLISECONDS);
//			executor.scheduleWithFixedDelay(t1, 0, 1000, TimeUnit.MILLISECONDS);
		}
	}
}
